﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using Microsoft.Office.Core;
using Powerpoint = Microsoft.Office.Interop.PowerPoint;
using System.Threading;
using System.Collections;
using System.Net.Sockets;
using System.Windows.Threading;
using System.Net;
using System.IO;
using System.Data.SqlClient;
using System.Data;
using System.Media;
using System.ComponentModel;
using Microsoft.VisualBasic;
//using System.Windows.Forms.Timer;

namespace AlgoLogicMessengerCompetitor
{
    /// <summary>
    /// Interaction logic for QuestionAnswer.xaml
    /// </summary>
    public partial class QuestionAnswer : Page
    {
        public int seconds; // Seconds.
        public int minutes; // Minutes.
        public int hours;   // Hours.
        public bool paused; // State of the timer [PAUSED/WORKING].
        public bool stop; //Stop the timer


        //--Display Question
        string showQ = "";
        string showA = "";
        string showB = "";
        string showC = "";
        string showD = "";//--

        //--Display Time
        string min = "";
        string sec = "";//--

        string SHOWQ = null;
        string SHOWA = null;
        string SHOWB = null;
        string SHOWC = null;
        string SHOWD = null;

        string MIN = null;
        string SEC = null;
        DispatcherTimer timer1 = new DispatcherTimer();
        DispatcherTimer ansboxtimer = new DispatcherTimer();
        Thread thread;
        bool IsThreadRunning = false;
        ReaderWriterLock rwl = new ReaderWriterLock();
        ArrayList userList = new ArrayList();


        //sockets
        System.Net.Sockets.TcpClient clientSocket = new System.Net.Sockets.TcpClient();
        NetworkStream serverStream = default(NetworkStream);

        string readData = null;
        string massConvoReadData = null;
        string num = null;
        string m = null;
        string s = null;
        string qns = null;
        string ans = null;
        string optA = null;
        string optB = null;
        string optC = null;
        string optD = null;

        List<BitmapImage> imageList = new List<BitmapImage>();
        int ClickCount = 0;

        BackgroundWorker worker = new BackgroundWorker();

        public QuestionAnswer()
        {
            InitializeComponent();
            ansboxtimer.Tick += new EventHandler(timer1_Tick);
            ansboxtimer.Interval = new TimeSpan(0, 0, 1);
            string ip = IP.Address;
            clientSocket.Connect(IP.Address, 8888);
            serverStream = clientSocket.GetStream();
            byte[] outStream = System.Text.Encoding.ASCII.GetBytes("" + "#");
            serverStream.Write(outStream, 0, outStream.Length);
            serverStream.Flush();

            worker.DoWork += delegate(object s, DoWorkEventArgs args)
            {
                getMessage();
            };
            worker.RunWorkerAsync(5000);

            TeamName.Content = IP.teamName;
            TeamScore.Content = "0";
        }

        int calclick = 1;
        private void Calcul_Click(object sender, RoutedEventArgs e)
        {
            if (System.Diagnostics.Process.GetProcessesByName("calc").Length >= 1)
            {
                MessageBox.Show("Calculator is Opened!", "Notification", MessageBoxButton.OK, MessageBoxImage.Information);
            }
            else
            {
                System.Diagnostics.Process.Start("calc");
            }
        }

        string points = "0";
        bool result;
        private void getMessage()
        {
            while (true)
            {
                try
                {
                    serverStream = clientSocket.GetStream();
                    int bufferSize = clientSocket.ReceiveBufferSize;
                    byte[] inStream = new byte[bufferSize];
                    serverStream.Read(inStream, 0, bufferSize);
                    string returndata = System.Text.Encoding.ASCII.GetString(inStream);

                    min = "";
                    sec = "";

                    //filter type of msg
                    string[] filterMsgType = returndata.Split('|');
                    string str = System.Text.Encoding.ASCII.GetString(inStream, 0, inStream.Length);
                    str = str.Replace("\0", "");
                    string[] array1 = str.Split('|');

                    if (array1[1].ToString() == "TeamCreated!")
                    {
                        returndata = "";
                    }

                    if (str.StartsWith("@ShowQuestion"))
                    {
                        returndata = array1[1].ToString() + ": " + array1[2].ToString() + Environment.NewLine;

                        string num = array1[2].Remove(0, 9).ToString();
                        string connection = array1[3].Replace(".", IP.facilitatorCom);
                        string image = array1[4].ToString();
                        string question = array1[5].ToString();
                        string showoptA = array1[6].ToString();
                        string showoptB = array1[7].ToString();
                        string showoptC = array1[8].ToString();
                        string showoptD = array1[9].ToString();
                        getImage(connection, num, image, question, showoptA, showoptB, showoptC, showoptD);
                        readData = "" + returndata;
                        msgTalk();
                    }

                    if (str.StartsWith("@HideQuestion"))
                    {
                        this.Dispatcher.Invoke(System.Windows.Threading.DispatcherPriority.Normal,
                      new Action(delegate()
                      {
                          Question.Text = "";
                          A.Text = "A) ";
                          B.Text = "B) ";
                          C.Text = "C) ";
                          D.Text = "D) ";
                          labNo.Content = "Question " + array1[1].ToString() + ": ";
                          imageExcel.Source = null;
                          compimageoptA.Source = null;
                          compimageoptB.Source = null;
                          compimageoptC.Source = null;
                          compimageoptD.Source = null;

                          imageExcel.Source = null;
                          imageExcel.Margin = new Thickness(77, 569, 0, 0);
                          BtnA.Margin = new Thickness(257, 459, 0, 0);
                          BtnB.Margin = new Thickness(0, 460, 659, 0);
                          BtnC.Margin = new Thickness(257, 494, 0, 0);
                          BtnD.Margin = new Thickness(319, 493, 0, 0);
                          LabCal.Margin = new Thickness(123, 444, 0, 0);
                          Calculator.Margin = new Thickness(77, 423, 0, 196);

                      }
                        )
                        );
                    }

                    if (str.StartsWith("@comConv"))
                    {
                        returndata = array1[1].ToString() + ": " + array1[2].ToString() + Environment.NewLine;

                        num = array1[2].Remove(0, 9).ToString();
                        string connection = array1[3].Replace(".", IP.facilitatorCom);
                        string image = array1[5].ToString();
                        string qns = array1[6].ToString();
                        string showA = array1[7].ToString();
                        string showB = array1[8].ToString();
                        string showC = array1[9].ToString();
                        string showD = array1[10].ToString();
                        getImage(connection, num, image, qns, showA, showB, showC, showD);
                        readData = "" + returndata;
                        msgTalk();
                    }
                    if (str.StartsWith("@EndComConv"))
                    {
                        returndata = array1[1].ToString() + ":" + array1[2].ToString() + Environment.NewLine;
                    }
                    //else if (str.StartsWith("@faster"))
                    //{
                    //    if (array1[1].ToString() != "")
                    //    {
                    //        returndata = "You Scored 1 Point!" + Environment.NewLine;
                    //    }
                    //    else
                    //    {
                    //        returndata = "None of the Team Scored!" + Environment.NewLine;
                    //    }
                    //    btnClick = 1;
                    //    readData = "" + returndata;
                    //    msg();

                    //}
                    else if (str.StartsWith("@comAnsConv"))
                    {
                        returndata = "[" + array1[3].ToString() + "] " + array1[4].ToString() + Environment.NewLine;
                        num = array1[3].Remove(0, 9).ToString();
                        string connection = array1[2].Replace(".", IP.facilitatorCom);
                        string ans = array1[5].ToString();
                        //  (new System.Threading.Thread(CloseIt)).Start();                        
                      //  AutoClosingMessageBox.Show("The correct answer is : " + ans, "  This message box will close in 5 seconds  " , 5000);

                        getAnsImage(connection, num, ans);
                        readData = "" + returndata;
                        msgTalk();
                    }
                    else if (str.StartsWith("@massConvo"))
                    {
                        //if (array1[1].ToString() == IP.teamName)
                        //{
                        //returndata = array1[1].ToString() + ": " + array1[2].ToString() + Environment.NewLine + "Time Left: " + array1[3].ToString() + " Seconds" + Environment.NewLine;
                        returndata = array1[1].ToString() + ": " + array1[2].ToString() + Environment.NewLine;
                        readData = "" + returndata;
                        msgTalk();
                        //}
                        //else
                        //{
                        //    returndata = "";
                        //}
                    }
                    else if (str.StartsWith("@displayTime"))
                    {
                        readData = "";

                        min = array1[1].ToString();
                        sec = array1[2].ToString();
                        MIN = min;
                        SEC = sec;

                        readData = "" + returndata;
                        displayTime();
                    }
                    else if (str.StartsWith("@time"))
                    {
                        returndata = "";

                        min = array1[1].ToString();
                        sec = array1[2].ToString();

                        result = false;
                        ansboxtimer.Start();

                        minutes = System.Convert.ToInt32(min);
                        seconds = System.Convert.ToInt32(sec);

                        this.Dispatcher.Invoke(System.Windows.Threading.DispatcherPriority.Normal,
                        new Action(delegate()
                        {
                            BtnA.IsEnabled = true;
                            BtnB.IsEnabled = true;
                            BtnC.IsEnabled = true;
                            BtnD.IsEnabled = true;
                        }
                          )
                          );
                        readData = "" + returndata;
                        msgTalk();
                    }

                    else if (str.StartsWith("@resumePause"))
                    {

                        ansboxtimer.Start();
                        returndata = "";

                        paused = Convert.ToBoolean(array1[1]);

                        this.Dispatcher.Invoke(System.Windows.Threading.DispatcherPriority.Normal,
                        new Action(delegate()
                        {
                            if (BtnA.IsEnabled == true)
                            {
                                BtnA.IsEnabled = true;
                                BtnB.IsEnabled = true;
                                BtnC.IsEnabled = true;
                                BtnD.IsEnabled = true;
                            }

                            pauseScreenComp.Visibility = System.Windows.Visibility.Hidden;
                        }
                            )
                            );
                        m = "" + min;
                        s = "" + sec;
                        time();

                    }

                    else if (str.StartsWith("@pause"))//if the server displays @pause, pause button activated on competitor side
                    {
                        min = array1[1].ToString();
                        sec = array1[2].ToString();

                        result = false;
                        ansboxtimer.Stop();



                        minutes = System.Convert.ToInt32(min);
                        seconds = System.Convert.ToInt32(sec);

                        this.Dispatcher.Invoke(System.Windows.Threading.DispatcherPriority.Normal,
                       new Action(delegate()
                       {
                           pauseScreenComp.Visibility = System.Windows.Visibility.Visible;
                       }
                         )
                         );
                        m = "" + min;
                        s = "" + sec;
                        time();

                    }

                    else if (str.StartsWith("@stop"))
                    {
                        min = array1[1].ToString();
                        sec = array1[2].ToString();

                        result = false;
                        ansboxtimer.Stop();

                        minutes = System.Convert.ToInt32(min);
                        seconds = System.Convert.ToInt32(sec);

                        this.Dispatcher.Invoke(System.Windows.Threading.DispatcherPriority.Normal,
                        new Action(delegate()
                        {
                            BtnA.IsEnabled = false;
                            BtnB.IsEnabled = false;
                            BtnC.IsEnabled = false;
                            BtnD.IsEnabled = false;
                        }
                            )
                            );
                        m = "" + min;
                        s = "" + sec;
                        time();

                    }

                    else if (str.StartsWith("@back"))
                    {
                        this.Dispatcher.BeginInvoke(System.Windows.Threading.DispatcherPriority.Normal,
                        new Action(delegate()
                        {
                            QNAFrame.Content = "";
                            QNAFrame.NavigationService.RemoveBackEntry();
                            NavigationPage np = new NavigationPage();
                            QNAFrame.Content = np;
                            //QNAFrame.Content = "";
                            //NavigationService nav = NavigationService.GetNavigationService(this);
                            //nav.Navigate(new System.Uri("NavigationPage.xaml", UriKind.RelativeOrAbsolute));
                        }
                          )
                          );
                    }
                    else if (str.StartsWith("@quizStart"))
                    {
                        returndata = "";
                    }
                    else if (str.StartsWith("@Score"))
                    {
                        returndata = "";
                        this.Dispatcher.Invoke(System.Windows.Threading.DispatcherPriority.Normal,
                        new Action(delegate()
                        {
                            if (array1[1] == IP.teamName)
                            {
                                string totalScore = array1[2].ToString();
                                TeamScore.Content = totalScore;
                            }
                        }
                          )
                          );
                    }
                    else if (str.StartsWith("@AllowNavigation"))
                    {
                        returndata = "";
                    }
                    else if (str.StartsWith("@DisallowNavigation"))
                    {
                        returndata = "";
                    }
                    else if (str.StartsWith("@ForceAllLogOut"))
                    {
                        this.Dispatcher.Invoke(System.Windows.Threading.DispatcherPriority.Normal,
                        new Action(delegate()
                        {
                            Environment.Exit(0);
                        }
                          )
                          );
                    }
                    else if (str.StartsWith("@Talk")) //Chat (Facilitator)
                    {
                        returndata = "Facilitator: " + array1[1] + Environment.NewLine;

                        readData = "" + returndata;
                        msgTalk();
                    }
                    else if (str.StartsWith("@talkConvo")) //Chat (Competitor)
                    {
                        returndata = array1[1].ToString() + ": " + array1[2].ToString() + Environment.NewLine;
                        readData = "" + returndata;
                        msgTalk();
                    }

                    else if (str.StartsWith("@checkVulgar")) //Chat (Competitor)
                    {
                        returndata = array1[1].ToString() + ": " + array1[2].ToString() + Environment.NewLine;
                        readData = "" + returndata;
                    }

                    else if (str.StartsWith("@displayLabel"))
                    {
                        if (array1[1] == IP.teamName)
                        {
                            //MessageBox.Show("Please do not use foul language");
                            this.Dispatcher.Invoke(System.Windows.Threading.DispatcherPriority.Normal,
                    new Action(delegate()
                    {

                        System.Windows.Forms.Timer labeltimer;
                        labeltimer = new System.Windows.Forms.Timer();
                        labeltimer.Interval = 5000;
                        labeltimer.Enabled = true;
                        labeltimer.Tick += new System.EventHandler(OnTimerEvent);

                        lblWarning.Content = "Please do not use foul language";
                    }
                      )
                      );
                        }
                    }
                }
                catch (Exception ex)
                {
                    MessageBox.Show(ex.Message);
                }
            }
        }
        private void displayTime()
        {
            if (this.Dispatcher.Thread == Thread.CurrentThread)
            {
                if (readData != null)
                {
                    if (readData != "")
                    {
                        Min.Content = MIN;
                        Sec.Content = SEC;
                    }
                }
            }
            else
            {
                Dispatcher.Invoke(new Action(displayTime));
            }
        }

        private void msgTalk() //Chat (facilitator)
        {
            if (this.Dispatcher.Thread == Thread.CurrentThread)
            {
                if (readData != null)
                {
                    if (readData != "")
                    {
                        tbxConvo.Text = tbxConvo.Text + Environment.NewLine + readData;
                        tbxConvo.SelectionStart = tbxConvo.Text.Length;
                        tbxConvo.ScrollToEnd();
                    }
                }
            }
            else
            {
                Dispatcher.Invoke(new Action(msgTalk));
            }
        }
        private void time()
        {
            this.Dispatcher.Invoke(
                System.Windows.Threading.DispatcherPriority.Normal,
                new Action(
                    delegate()
                    {
                        Min.Content = m;
                        Sec.Content = s;
                    }
                )
                );
        }

        private void getImage(string connection, string QNum, string image, string question, string optionA, string optionB, string optionC, string optionD)
        {
            this.Dispatcher.Invoke(
                System.Windows.Threading.DispatcherPriority.Normal,
                new Action(
                    delegate()
                    {
                        labNo.Content = "Question " + QNum + ": ";
                        Question.Text = question;

                        string fulloptA = "C:\\Images\\" + optionA;

                        FileInfo file = new FileInfo(optionA);
                        string extension = file.Extension;
                        if (extension == ".jpg" || extension == ".png")
                        {
                            BitmapImage src1 = new BitmapImage();
                            src1.BeginInit();
                            src1.UriSource = new Uri(fulloptA);
                            //src1.UriSource = new Uri("C:\\Images\\" + optionA, UriKind.Relative);
                            src1.CacheOption = BitmapCacheOption.OnLoad;
                            src1.EndInit();
                            compimageoptA.Source = src1;
                            A.Text = "A) ";
                        }
                        else
                        {
                            A.Text = "A) " + optionA;
                            compimageoptA.Source = null;
                        }

                        string fulloptB = "C:\\Images\\" + optionB;
                        FileInfo file3 = new FileInfo(optionB);
                        string extension3 = file3.Extension;
                        if (extension3 == ".jpg" || extension3 == ".png")
                        {
                            BitmapImage src3 = new BitmapImage();
                            src3.BeginInit();
                            src3.UriSource = new Uri(fulloptB);
                            //src3.UriSource = new Uri("C:\\Images\\" + B, UriKind.Relative);
                            src3.CacheOption = BitmapCacheOption.OnLoad;
                            src3.EndInit();
                            compimageoptB.Source = src3;
                            B.Text = "B) ";
                        }
                        else
                        {
                            B.Text = "B) " + optionB;
                            compimageoptB.Source = null;
                        }

                        string fulloptC = "C:\\Images\\" + optionC;
                        FileInfo file4 = new FileInfo(optionC);
                        string extension4 = file4.Extension;
                        if (extension4 == ".jpg" || extension4 == ".png")
                        {
                            BitmapImage src4 = new BitmapImage();
                            src4.BeginInit();
                            src4.UriSource = new Uri(fulloptC);
                            //src4.UriSource = new Uri("C:\\Images\\" + C, UriKind.Relative);
                            src4.CacheOption = BitmapCacheOption.OnLoad;
                            src4.EndInit();
                            compimageoptC.Source = src4;
                            C.Text = "C) ";
                        }
                        else
                        {
                            C.Text = "C) " + optionC;
                            compimageoptC.Source = null;
                        }

                        string fulloptD = "C:\\Images\\" + optionD;

                        FileInfo file5 = new FileInfo(fulloptD);
                        string extension5 = file5.Extension;
                        if (extension5 == ".jpg" || extension5 == ".png")
                        {
                            BitmapImage src5 = new BitmapImage();
                            src5.BeginInit();
                            src5.UriSource = new Uri(fulloptD);
                            //src5.UriSource = new Uri("C:\\Images\\" + optionD, UriKind.Relative);
                            //getim = ("C:\\Images\\" + optionD);
                            src5.CacheOption = BitmapCacheOption.OnLoad;
                            src5.EndInit();
                            compimageoptD.Source = src5;
                            D.Text = "D) ";
                        }
                        else
                        {
                            D.Text = "D) " + optionD;
                            compimageoptD.Source = null;
                        }


                        if (image != "-")
                        {
                            BitmapImage src = new BitmapImage();
                            src.BeginInit();
                            src.UriSource = new Uri("C:\\Images\\" + image, UriKind.Relative);
                            src.CacheOption = BitmapCacheOption.OnLoad;
                            src.EndInit();
                            imageExcel.Source = src;

                            imageExcel.Margin = new Thickness(77, 432, 0, 0);
                            BtnA.Margin = new Thickness(257, 650, 0, 0);
                            BtnB.Margin = new Thickness(0, 649, 659, 0);
                            BtnC.Margin = new Thickness(257, 685, 0, 0);
                            BtnD.Margin = new Thickness(319, 685, 0, 0);
                            LabCal.Margin = new Thickness(123, 617, 0, 0);
                            Calculator.Margin = new Thickness(77, 596, 0, 23);
                        }
                        else
                        {
                            imageExcel.Source = null;
                            imageExcel.Margin = new Thickness(77,569,0,0);
                            BtnA.Margin = new Thickness(257, 459, 0, 0);
                            BtnB.Margin = new Thickness(0, 460, 659, 0);
                            BtnC.Margin = new Thickness(257, 494, 0, 0);
                            BtnD.Margin = new Thickness(319, 493, 0, 0);
                            LabCal.Margin = new Thickness(123, 444, 0, 0);
                            Calculator.Margin = new Thickness(77, 423, 0, 196);
                        }
                    }
                )
                );
        }

        private void getAnsImage(string connection, string QNum, string answer)
        {
            this.Dispatcher.Invoke(
                System.Windows.Threading.DispatcherPriority.Normal,
                new Action(
                    delegate()
                    {
                        ans = answer;
                    }
                )
                );
        }

        private void btnSend_Click(object sender, RoutedEventArgs e)
        {
            string talk = (sendbox.Text.Trim());
            if (talk != "")
            {
                string msg2 = "@checkVulgar|" + IP.teamName + "|" + talk + "|";
                //string msg2 = "@talkConvo|" + IP.teamName + "|" + talk + "|";
                byte[] outStream2 = System.Text.Encoding.ASCII.GetBytes(msg2 + "#");
                if (serverStream != null)
                {
                    serverStream.Write(outStream2, 0, outStream2.Length);
                    serverStream.Flush();
                }
                sendbox.Clear();
            }
            else
            {
                MessageBox.Show("Please Type a Message!", "Notification", MessageBoxButton.OK, MessageBoxImage.Information);
            }
        }

        string message = null;

        private void BtnA_Click(object sender, RoutedEventArgs e)
        {
            BtnA.IsEnabled = true;
            BtnB.IsEnabled = true;
            BtnC.IsEnabled = true;
            BtnD.IsEnabled = true;
            if (MessageBox.Show("Do You Want To Submit Option: " + BtnA.Content + " ?", "Confirmation", MessageBoxButton.YesNo) == MessageBoxResult.Yes)
            {
                MessageBox.Show("Your Answer Has Been Submitted!", "Notification", MessageBoxButton.OK, MessageBoxImage.Information);
                //int time_m = Convert.ToInt32(Min.Content.ToString()) * 60;
                //int time_s = Convert.ToInt32(Sec.Content.ToString());
                //int time_t = time_m + time_s;
                //string time = time_t.ToString();
                string time = DateTime.Now.ToLongTimeString();
                message = "@massConvo|" + IP.teamName + "|" + BtnA.Content + "|" + time;
                byte[] outStream = System.Text.Encoding.ASCII.GetBytes(message + "#");
                if (serverStream != null)
                {
                    serverStream.Write(outStream, 0, outStream.Length);
                    serverStream.Flush();
                }
                BtnA.IsEnabled = false;
                BtnB.IsEnabled = false;
                BtnC.IsEnabled = false;
                BtnD.IsEnabled = false;
            }
        }

        private void BtnB_Click(object sender, RoutedEventArgs e)
        {
            BtnA.IsEnabled = true;
            BtnB.IsEnabled = true;
            BtnC.IsEnabled = true;
            BtnD.IsEnabled = true;

            if (MessageBox.Show("Do You Want To Submit Option: " + BtnB.Content + " ?", "Confirmation", MessageBoxButton.YesNo) == MessageBoxResult.Yes)
            {
                MessageBox.Show("Your Answer Has Been Submitted!", "Notification", MessageBoxButton.OK, MessageBoxImage.Information);
                //int time_m = Convert.ToInt32(Min.Content.ToString()) * 60;
                //int time_s = Convert.ToInt32(Sec.Content.ToString());
                //int time_t = time_m + time_s;
                //string time = time_t.ToString();
                string time = DateTime.Now.ToLongTimeString();
                message = "@massConvo|" + IP.teamName + "|" + BtnB.Content + "|" + time;
                byte[] outStream = System.Text.Encoding.ASCII.GetBytes(message + "#");
                if (serverStream != null)
                {
                    serverStream.Write(outStream, 0, outStream.Length);
                    serverStream.Flush();
                }
                BtnA.IsEnabled = false;
                BtnB.IsEnabled = false;
                BtnC.IsEnabled = false;
                BtnD.IsEnabled = false;
            }
        }

        private void BtnC_Click(object sender, RoutedEventArgs e)
        {
            BtnA.IsEnabled = true;
            BtnB.IsEnabled = true;
            BtnC.IsEnabled = true;
            BtnD.IsEnabled = true;

            if (MessageBox.Show("Do You Want To Submit Option: " + BtnC.Content + " ?", "Confirmation", MessageBoxButton.YesNo) == MessageBoxResult.Yes)
            {
                MessageBox.Show("Your Answer Has Been Submitted!", "Notification", MessageBoxButton.OK, MessageBoxImage.Information);
                //int time_m = Convert.ToInt32(Min.Content.ToString()) * 60;
                //int time_s = Convert.ToInt32(Sec.Content.ToString());
                //int time_t = time_m + time_s;
                //string time = time_t.ToString();
                string time = DateTime.Now.ToLongTimeString();
                message = "@massConvo|" + IP.teamName + "|" + BtnC.Content + "|" + time;
                byte[] outStream = System.Text.Encoding.ASCII.GetBytes(message + "#");
                if (serverStream != null)
                {
                    serverStream.Write(outStream, 0, outStream.Length);
                    serverStream.Flush();
                }
                BtnA.IsEnabled = false;
                BtnB.IsEnabled = false;
                BtnC.IsEnabled = false;
                BtnD.IsEnabled = false;
            }
        }

        private void BtnD_Click(object sender, RoutedEventArgs e)
        {
            BtnA.IsEnabled = true;
            BtnB.IsEnabled = true;
            BtnC.IsEnabled = true;
            BtnD.IsEnabled = true;

            if (MessageBox.Show("Do You Want To Submit Option: " + BtnD.Content + " ?", "Confirmation", MessageBoxButton.YesNo) == MessageBoxResult.Yes)
            {
                MessageBox.Show("Your Answer Has Been Submitted!", "Notification", MessageBoxButton.OK, MessageBoxImage.Information);
                //int time_m = Convert.ToInt32(Min.Content.ToString()) * 60;
                //int time_s = Convert.ToInt32(Sec.Content.ToString());
                //int time_t = time_m + time_s;
                //string time = time_t.ToString();
                string time = DateTime.Now.ToLongTimeString();
                message = "@massConvo|" + IP.teamName + "|" + BtnD.Content + "|" + time;
                byte[] outStream = System.Text.Encoding.ASCII.GetBytes(message + "#");
                if (serverStream != null)
                {
                    serverStream.Write(outStream, 0, outStream.Length);
                    serverStream.Flush();
                }
                BtnA.IsEnabled = false;
                BtnB.IsEnabled = false;
                BtnC.IsEnabled = false;
                BtnD.IsEnabled = false;
            }
        }

        private void timer1_Tick(object sender, EventArgs e)
        {
            // Verify if the time didn't pass.
            if ((minutes == 0) && (seconds == 0))
            {
                // If the time is over, clear all settings and fields.
                // Also, show the message, notifying that the time is over.
                ansboxtimer.Stop();

                Min.Content = "00";
                Sec.Content = "00";

                //Message.Content = "Time's Up !";
                BtnA.IsEnabled = false;
                BtnB.IsEnabled = false;
                BtnC.IsEnabled = false;
                BtnD.IsEnabled = false;
            }
            else
            {
                // Else continue counting.
                //btnSubmit.IsEnabled = true;
                if (seconds < 1)
                {
                    seconds = 59;
                    if (minutes == 0)
                    {
                        minutes = 59;
                    }
                    else
                    {
                        minutes -= 1;
                    }
                }
                else if (seconds <= 10)
                {
                    seconds -= 1;

                    //Message.Content = "Hurry Up! 10 More Seconds Left!";
                }
                else
                {
                    seconds -= 1;
                    //Message.Content = "Answer the Question";
                }
            }
            // Display the current values of hours, minutes and seconds in
            // the corresponding fields.
            if (minutes < 10)
            {
                Min.Content = "0" + minutes.ToString();
            }
            else
            {
                Min.Content = minutes.ToString();
            }
            if (seconds < 10)
            {
                Sec.Content = "0" + seconds.ToString();
            }
            else
            {
                Sec.Content = seconds.ToString();
            }

        }

        private void Page_PreviewKeyDown(object sender, KeyEventArgs e)
        {
            if (e.Key == Key.Back)
            {
                if (sendbox.Text != "")
                {
                    e.Handled = false;
                }
                else
                {
                    e.Handled = true;
                }
            }
            else if (e.Key == Key.F5)
            {
                e.Handled = true;
            }
        }

        private void tbxmsg_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.Key == Key.Enter)
            {
                btnSend_Click(this, new RoutedEventArgs());
            }
        }

        private void image1_ImageFailed(object sender, ExceptionRoutedEventArgs e)
        {

        }

        //add nonsense
        private void OnTimerEvent(object sender, EventArgs e)
        {
            lblWarning.Content = "";
        }

       //// public class AutoClosingMessageBox
       // {
       //     System.Threading.Timer msgbxTimer;
       //     string _caption;
       //     AutoClosingMessageBox(string text, string caption, int timeout)
       //     {
       //         _caption = caption;
       //         msgbxTimer = new System.Threading.Timer(OnTimerElapsed,
       //             null, timeout, System.Threading.Timeout.Infinite);
       //         MessageBox.Show(text, caption);
       //     }
       //     public static void Show(string text, string caption, int timeout)
       //     {
       //         new AutoClosingMessageBox(text, caption, timeout);
       //     }
       //     void OnTimerElapsed(object state)
       //     {
       //         IntPtr mbWnd = FindWindow(null, _caption);
       //         if (mbWnd != IntPtr.Zero)
       //             SendMessage(mbWnd, WM_CLOSE, IntPtr.Zero, IntPtr.Zero);
       //         msgbxTimer.Dispose();
       //     }
       //     const int WM_CLOSE = 0x0010;
       //     [System.Runtime.InteropServices.DllImport("user32.dll", SetLastError = true)]
       //     static extern IntPtr FindWindow(string lpClassName, string lpWindowName);
       //     [System.Runtime.InteropServices.DllImport("user32.dll", CharSet = System.Runtime.InteropServices.CharSet.Auto)]
       //     static extern IntPtr SendMessage(IntPtr hWnd, UInt32 Msg, IntPtr wParam, IntPtr lParam);
       // }
        

    }
}